package restful

import (
	"context"

	"gitee.com/go-course/go11/vblog/apps/blog"
	"github.com/infraboard/mcube/client/rest"
)

var _ blog.Service = &BlogClient{}

type BlogClient struct {
	c *rest.RESTClient
}

// 查询文章列表
func (c *BlogClient) QueryBlog(ctx context.Context, in *blog.QueryBlogRequest) (*blog.BlogSet, error) {
	return nil, nil
}

// // 查询单个文章
// func (c *BlogClient) DescribeBlog(ctx context.Context, in *blog.DescribeBlogRequest) (*blog.Blog, error) {
// 	// 构造一个http Request
// 	// http, put, post, delete
// 	req, err := http.NewRequest("GET", c.c.Address+"/vblog/api/v1/blogs/"+in.Id, nil)
// 	if err != nil {
// 		return nil, err
// 	}
// 	req.AddCookie(&http.Cookie{
// 		Name:  "token",
// 		Value: c.c.Token,
// 	})

// 	resp, err := c.c.client.Do(req)
// 	if err != nil {
// 		return nil, err
// 	}

// 	respBytes, err := io.ReadAll(resp.Body)
// 	if err != nil {
// 		return nil, err
// 	}
// 	defer resp.Body.Close()

// 	fmt.Println(string(respBytes))

// 	// 反序列化
// 	ins := blog.NewBlog(blog.NewCreateBlogRequest())
// 	err = json.Unmarshal(respBytes, ins)
// 	if err != nil {
// 		return nil, err
// 	}
// 	return ins, nil
// }

// 查询单个文章
func (c *BlogClient) DescribeBlog(ctx context.Context, in *blog.DescribeBlogRequest) (*blog.Blog, error) {
	// c.c.Address+"/vblog/api/v1/blogs/"+in.Id
	ins := blog.NewBlog(blog.NewCreateBlogRequest())
	err := c.c.
		Get("/vblog/api/v1/blogs/").
		Suffix(in.Id).
		Do(ctx).
		Into(ins)
	if err != nil {
		return nil, err
	}
	return ins, nil
}

// 接口一定要保证很强一个兼容性
func (c *BlogClient) CreateBlog(ctx context.Context, in *blog.CreateBlogRequest) (*blog.Blog, error) {
	return nil, nil
}

// 更新文章
func (c *BlogClient) UpdateBlog(ctx context.Context, in *blog.UpdateBlogRequest) (*blog.Blog, error) {
	return nil, nil
}

// 删除文章, 返回删除的对象, 用前端提升, 用于对象最终
func (c *BlogClient) DeleteBlog(ctx context.Context, in *blog.DeleteBlogRequest) (*blog.Blog, error) {
	return nil, nil
}
